package net.edu_soft.ec.asset.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.RoleMapper;
import net.edu_soft.ec.asset.pojo.Role;
import net.edu_soft.ec.asset.pojo.RoleExample;
import net.edu_soft.ec.asset.service.RoleService;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role find(Integer roleId) {
        PuingUtil.notNullByZero(roleId, "角色管理：角色id不能为空或小于等于0");
        return sel(roleId);
    }

    @Override
    public PageInfo<Role> findList(Integer pageNum, Integer pageSize) {
        PuingUtil.notNullByInt("角色管理:分页参数不合法", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<Role> roles = roleMapper.selectByExample(null);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return pageInfo;
    }

    @Override
    public int save(String name, String details) {
        Assert.hasLength(name, "角色管理：添加的角色名不能为空");
        Assert.hasLength(details, "角色管理：添加的描述名不能为空");
        RoleExample example = new RoleExample();
        //大于0则存在该角色
        if (sel(name) != null) {
            throw new ParameterException("角色管理：添加的角色名：" + name + " 已与数据库中重复！");
        }
        Role role = new Role();
        role.setName(name);
        role.setDetails(details);
        role.setStatus(EduConstant.STATUS_ZREO);
        return roleMapper.insertSelective(role);
    }

    @Override
    public int update(Role role) {
        PuingUtil.notNullByZero(role.getRoleId(), "角色管理：角色id不能为空或小于等于0");
        Assert.hasLength(role.getDetails(), "角色管理：添加的描述名不能为空");
        Role role1 = roleMapper.selectByPrimaryKey(role.getRoleId());
        if (role1 != null) {
            //根据姓名查询出的角色信息的id与要修改的角色的id，不相等为存在该角色名字，不可以修改
            Role role2 = sel(role.getName());
            if (role2.getRoleId() != role1.getRoleId()) {
                throw new ParameterException("角色管理。修改失败，该角色名：" + role.getName() + " 已存在");
            }
        } else {
            throw new ParameterException("角色管理。修改失败， 角色不存在");
        }
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int del(Integer roleId, Integer isDel) {
        int i = 0;
        //删除
        if (isDel == 1  && isDel != null) {
            Role role = sel(roleId);
            if(role==null){
                throw new ParameterException("角色管理：删除失败， 角色不存在");
            }
            role.setStatus(EduConstant.STATUS_ONE);//1 删除
            i = roleMapper.updateByPrimaryKeySelective(role);
        }
        return i;
    }

    private Role sel(String name) {
        Assert.hasLength(name, "角色管理：添加的角色名不能为空");
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        //添加姓名
        criteria.andNameEqualTo(name);
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<Role> roles = roleMapper.selectByExample(example);
        //大于0则存在该角色
        if (roles.size() > 0) {
            return roles.get(0);
        }
        return null;
    }

    private Role sel(Integer id) {
        PuingUtil.notNullByZero(id, "角色管理：角色id不能为空或小于等于0");
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        //添加id
        criteria.andRoleIdEqualTo(id);
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<Role> roles = roleMapper.selectByExample(example);
        //大于0则存在该角色
        if (roles.size() > 0) {
            return roles.get(0);
        }
        return null;
    }

}
